<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Class Designer
 * 设计师模型
 * Date: 2018/12/5 0005
 * Time: 15:23
 */

namespace app\index\model;



class Cases extends Base
{
    protected $table = 'shop_case';

    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:施工案例列表
     * @param string designer_level
     * @param string $classification_id(12=定制案例，13=施工案例)
     * @param string $layct
     * @return int|string
     */
    public function caseList($style="",$layct="",$is_top="",$classification_id="13",$structure="",$orders="",$after_limit = "15",$keyword=''){
        if($orders){
            $orderName = $orders["type"];
            $orderType = $orders["type_order"];
        }else{
            $orderName = "drawing_id";
            $orderType = "desc";
        }
        return $this->alias('a')->field('a.*,b.layct,b.style,b.no,b.layct,b.cover,b.zarea,b.jarea,b.bprice1,b.bprice2,b.depth,b.open,b.struct,c.config_name')->join('shop_drawing b','a.drawing_id = b.drawing_id','left')->join('shop_config c','b.style = c.config_id','left')->where(function ($q)use ($style){
            if(!empty($style)){
                //图纸风格
                $q->where("b.style",$style);
            }
            })
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    //模糊查找支持标题和编号
                    $q->whereOr("b.no",'like','%'.$keyword.'%')->whereOr("b.topic",'like','%'.$keyword.'%');
                }
            })
            ->where(function ($q)use ($layct){
                if(!empty($layct)){
                    //楼层
                    $q->where("b.layct",$layct);
                }
            })
            ->where(function ($q)use ($structure){
                if(!empty($structure)){
                    //楼层
                    $q->where("b.struct",$structure);
                }
            })
            ->where(function ($q)use ($is_top){
                if(!empty($is_top)){
                    $q->where("a.is_top",$is_top);
                }
            })
            ->where(function ($q)use ($classification_id){
                if(!empty($classification_id)){
                    $q->where("a.classification_id",$classification_id);
                }
            })
            ->where('a.delete',0)
            ->where('a.is_display',1)
            ->orderRaw("b.$orderName $orderType")
            ->paginate($after_limit,false,[
                'type'     => 'page\marketPage',
                'var_page'  => 'p',
                'query' =>  request()->param(),
            ]);
    }

    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:施工案例总数
     * @param string designer_level
     * @param string $cate
     * @param string $layct
     * @return int|string
     */
    public function caseListCount($style="",$layct="",$is_top="",$classification_id="13",$structure=""){
        return $this->alias('a')->join('shop_drawing b','a.drawing_id = b.drawing_id','left')->where(function ($q)use ($style){
            if(!empty($style)){
                //图纸风格
                $q->where("b.style",$style);
            }
            })
            ->where(function ($q)use ($layct){
                if(!empty($layct)){
                    //楼层
                    $q->where("b.layct",$layct);
                }
            })
            ->where(function ($q)use ($structure){
                if(!empty($structure)){
                    //楼层
                    $q->where("b.struct",$structure);
                }
            })
            ->where(function ($q)use ($is_top){
                if(!empty($is_top)){
                    $q->where("a.is_top",$is_top);
                }
            })
            ->where(function ($q)use ($classification_id){
                if(!empty($classification_id)){
                    $q->where("a.classification_id",$classification_id);
                }
            })
            ->where('a.delete',0)
            ->where('a.is_display',1)
            ->count();
    }

    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:施工案例列表
     * @param string designer_level
     * @param string $cate
     * @param string $layct
     * @return int|string
     */
    public function caseInfo($caseId){
        return $this->alias('a')->field('a.*,b.layct,b.style,b.drawing_atlas,b.no,b.layct,b.zarea,b.jarea,b.bprice1,b.bprice2,b.depth,b.open,b.struct,c.config_name')->join('shop_drawing b','a.drawing_id = b.drawing_id','left')->join('shop_config c','b.struct = c.config_id','left')
            ->where(function ($q)use ($caseId){
                if(!empty($caseId)){
                    //楼层
                    $q->where("a.case_id",$caseId);
                }
            })
            ->where('a.delete',0)
            ->find();
    }

    /**
     * Author:you
     * Time:2018-07-13 16:01:39
     * Describe:根据ID获取信息
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id){
        return $this->alias("a")->field("a.*")
            ->find($id);
    }

    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:右侧侧订制随机
     * @param string $keyword
     * @param string $cate
     * @param string $layct
     * @return int|string
     */
    public function leftCaseList($limit="3",$classification_id="12",$style=""){
        return $this->alias('a')->field('a.case_id,a.title,a.thumbnail,b.cover,b.is_shel,b.layct,b.style,c.config_name')->join('shop_drawing b','a.drawing_id = b.drawing_id','left')->join('shop_config c','b.style = c.config_id','left')
            ->where("a.classification_id",$classification_id)
            ->where("a.delete",0)
            ->where(function ($q)use ($style){
                if(!empty($style)){
                    //楼层
                    $q->where("b.style",$style);
                }
            })
            ->where('a.is_display',1)
            ->orderRaw('rand()')
            ->orderRaw("a.reading_number desc")
            ->limit($limit)
            ->select();
    }
}